package com.slb.slnwp.common.config;

import com.slb.slnwp.common.utils.SnowflakeldGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Random;

/**
 * @author: joinly
 * @date: 2022/3/11 17:07
 * @desc:
 **/
@Configuration
public class SnowflakeldGeneratorConfig {

    /**
     * Snowflake 算法,分布式唯一 ID 生成算法
     * @return
     */
    @Bean
    public SnowflakeldGenerator snowflakeldGenerator() {
        // 生成一个随机数，用于雪花算法使用
        Random rand = new Random();
        int workerId = rand.nextInt(9) + 1; // 0 - 9
        int datacenterId = rand.nextInt(26 ) + 5; //5 -30
        SnowflakeldGenerator generator = new SnowflakeldGenerator(workerId, datacenterId);
        return generator;
    }
}
